package com.openatc.extend.common.aop.log;

import com.google.gson.*;
import com.openatc.core.model.RESTRetBase;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：panleilei
 * @description
 * @modified By：
 * @version:
 * @date ：Created in 2023/3/28 14:00
 */
public abstract class AbstractLogDataExtractor implements DataExtractor {
    /**
     * 方法名与操作模块的映射
     */
    private static final Map<String,String> models = new HashMap<>(32);
    /**
     * 方法名与操作类型的映射
     */
    private static final Map<String,String> infotypes = new HashMap<>(32);

    /**
     * 模块名与所属项目的映射
     */
    private static final Map<String,Integer> project = new HashMap<>(32);
    @Override
    public abstract Object extract(ProceedingJoinPoint point, Object ret);

    /**
     * 操作记录所属平台：开源平台
     */
    protected static final Integer PROJECT_OPENSOURCE = 0;
    /**
     * 操作记录所属平台：商业版平台
     */
    protected static final Integer PROJECT_PRO = 1;

    protected static final String CREATE_INFOTYPE = "create";
    protected static final String MODIFY_INFOTYPE = "update";
    protected static final String DELETE_INFOTYPE = "delete";
    protected static final String DOWNLOAD_INFOTYPE = "download";
    protected static final String LOGIN_INFOTYPE = "login";
    protected static final String LOGOUT_INFOTYPE = "logout";

    /**
     * 获取模块映射
     * @return
     */
    protected Map<String,String> getModels() {
        return models;
    }

    /**
     * 添加方法名和模块映射
     * @param methodName 方法名
     * @param model 模块
     */
    protected static void addMethodNameModel(String methodName,String model) {
        models.put(methodName,model);
    }

    /**
     * 添加模块名与所属项目的映射
     * @param modelName
     * @param projectType
     */
    protected static void addModelNameProject(String modelName, Integer projectType) {
        project.put(modelName,projectType);
    }

    /**
     * 根据模块名获取项目类型
     * @param name
     * @return
     */
    protected Integer getRecordRequestSource(String name) {
        return project.get(name);
    }

    /**
     * 添加方法名和类型映射
     * @param methodName 方法名
     * @param type 类型
     */
    protected static void addMethodNameType(String methodName,String type) {
        infotypes.put(methodName,type);
    }

    /**
     * 获取请求方法对应的模块
     * @param name
     * @return
     */
    protected String getRecodeModel(String name) {
        return models.get(name);
    }

    /**
     * 获取操作类型
     * @param methodName
     * @return
     */
    protected String getInfoType(String methodName) {
        return infotypes.get(methodName);
    }



    /**
     * 获取方法的执行状态
     * @param ret
     * @return
     */
    protected int getStatus(Object ret) {
        if (ret instanceof RESTRetBase) {
            RESTRetBase retBase = (RESTRetBase) ret;
            return retBase.getCode().equals("0")? 0 : 1;
        } else {
            return 1;
        }
    }


    /**
     * 根据方法名获取对应的项目类型
     * @param methodName
     * @return
     */
    protected Integer getRequestSocurce(String methodName) {
        String recodeModel = getRecodeModel(methodName);
        return getRecordRequestSource(recodeModel);
    }

    /**
     * 获取方法的响应体
     * @param ret
     * @return
     */
    protected JsonElement getResponseBody(Object ret) {
        if (ret == null) return null;
        Gson gson = new GsonBuilder().create();
        if (ret instanceof RESTRetBase) {
            RESTRetBase retBase = (RESTRetBase) ret;
            return gson.toJsonTree(retBase);
        }
        JsonObject ex = new JsonObject();
        ex.addProperty("exception",ret.toString());
        return ex;
    }
}
